package qlsl.androiddesign.view.subview.activityview;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.qlsl.androiddesign.kuaiwa.R;

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import qlsl.androiddesign.activity.commonactivity.SelectCityActivity;
import qlsl.androiddesign.activity.subactivity.CopyActivity;
import qlsl.androiddesign.activity.subactivity.RepairActivity;
import qlsl.androiddesign.activity.subactivity.RepairDetailActivity;
import qlsl.androiddesign.activity.subactivity.RepairShoppingReleaseActivity;
import qlsl.androiddesign.activity.subactivity.TrailerDetailActivity;
import qlsl.androiddesign.activity.subactivity.TrailerReleaseActivity;
import qlsl.androiddesign.adapter.subadapter.TrailerAdapter;
import qlsl.androiddesign.constant.Constant;
import qlsl.androiddesign.entity.commonentity.City;
import qlsl.androiddesign.entity.commonentity.Pager;
import qlsl.androiddesign.entity.otherentity.Trailer;
import qlsl.androiddesign.http.service.subservice.ForumService;
import qlsl.androiddesign.http.service.subservice.TrailerService;
import qlsl.androiddesign.library.pulltorefresh.PullToRefreshListView;
import qlsl.androiddesign.listener.OnPullRefreshListener;
import qlsl.androiddesign.popupwindow.subwindow.ProvincePopupWindow;
import qlsl.androiddesign.popupwindow.subwindow.ServeTypePopupWindow;
import qlsl.androiddesign.view.baseview.FunctionView;
import qlsl.androiddesign.view.subview.commonview.TabView3;

/**
 * 快修页<br/>
 * 需要传入的键：<br/>
 * 传入的值类型： <br/>
 * 传入的值含义：<br/>
 * 是否必传 ：
 */
public class RepairView extends FunctionView<RepairActivity> {

	private PullToRefreshListView refreshView;

	private Pager pager;

	private List<Trailer> list = new ArrayList<Trailer>();

	private ServeTypePopupWindow provinceWindow;

	public RepairView(RepairActivity activity) {
		super(activity);
		setContentView(R.layout.activity_repair);
	}

	protected void initView(View view) {
		setTitle("快修");
		refreshView = findViewById(R.id.refreshListView);
		setRightButtonResource(R.drawable.forum_posth);

		showRightButton();

		provinceWindow = new ServeTypePopupWindow(activity);
	}

	protected void initData() {
		queryTrailerList(1, "", "");
	}

	public void queryTrailerList(int page, String city, String servertype) {// 服务类型
		TrailerService.queryQuickLyrepairList(page, "" + servertype, ""
				+ Constant.longitude, "" + Constant.latitude, city, "1", this,
				activity);
	}

	private void resetList(List<Trailer> list_result) {
		list.clear();
		list.addAll(list_result);
	}

	protected void initListener() {
		refreshView.setOnRefreshListener(refreshListener);
	}

	public <T> void showData(T... t) {
		if (t[0] instanceof HashMap) {
			HashMap<String, Object> map = (HashMap<String, Object>) t[0];
			List<Trailer> list_result = (List<Trailer>) map.get("list");
			pager = (Pager) map.get("pager");
			if (pager.getPageNo() == 1) {
				resetList(list_result);
			} else {
				list.addAll(list_result);
			}
			notifyDataSetChanged();
		}
	}

	private void notifyDataSetChanged() {
		BaseAdapter adapter = (BaseAdapter) refreshView.getBaseAdapter();
		if (adapter == null) {
			adapter = new TrailerAdapter(activity, list);
			refreshView.setAdapter(adapter);
		} else {
			adapter.notifyDataSetChanged();
		}
		refreshView.onRefreshComplete();
	}

	public <T> void showNoData(T... t) {

	}

	public void onClick(View view) {
		switch (view.getId()) {
		case R.id.list_item:
			doClickListItem(view);
			break;

		case R.id.tv_guide1:
			doClickChooseCityItem(view);
			break;
		case R.id.tv_guide2:
			doClickGuideItem(view);
			break;
		case R.id.btn_right:
			doClickReleaseItemView(view);
			break;
		case R.id.window_list_item:
			doClickWindownListItem(view);
			break;
		}
	}

	private void doClickWindownListItem(View view) {
		String serverType = provinceWindow.getSelectItem(view);
		provinceWindow.dismiss();
		queryTrailerList(1, "", "" + serverType);
	}

	private void doClickGuideItem(View view) {
		findViewById(R.id.tv_guide2).setActivated(true);
		findViewById(R.id.tv_guide1).setActivated(false);
		provinceWindow.showAsDropDown((ViewGroup) view.getParent());
	}

	private void doClickListItem(View view) {
		int position = refreshView.getRefreshableView()
				.getPositionForView(view);
		Trailer recruit = (Trailer) refreshView.getAdapter().getItem(position);

		activity.startActivity(recruit, RepairDetailActivity.class);
	}

	private void doClickReleaseItemView(View view) {
		startActivityForResult(RepairShoppingReleaseActivity.class, 0);
	}

	private void doClickChooseCityItem(View view) {
		findViewById(R.id.tv_guide1).setActivated(true);
		findViewById(R.id.tv_guide2).setActivated(false);

		Intent intent0 = new Intent(activity, SelectCityActivity.class);
		startActivityForResult(intent0, Constant.SELECT_CITY_RESULT_CODE);
	}

	private OnPullRefreshListener<ListView> refreshListener = new OnPullRefreshListener<ListView>() {

		public void onPullDown() {
			queryTrailerList(1, Constant.city, "");
		}

		public void onPullUp() {
			super.onPullUp(refreshView, pager);
		}

		public void nextPager() {
			queryTrailerList(pager.getPageNo() + 1, Constant.city, "");
		}
	};

	public void onActivityResult(int requestCode, int arg1,
			Intent data) {
		if (requestCode == Constant.SELECT_CITY_RESULT_CODE) {
			if (data != null) {
				City city = (City) data.getExtras().get("city");
				City city2 = (City) data.getExtras().get("city2");

				String append = null;
				if (city2 != null) {
					append = city2.getArea_name();
				}
				if (city != null) {
					if (!TextUtils.isEmpty(append)) {
						append += city.getArea_name();
					} else {
						append = city.getArea_name();
					}
					queryTrailerList(1, "" + append, "");
					// TextView aa = (TextView)
					// findViewById(R.id.order_address);
					// aa.setText("" + append);
				}
			}
		} else if (hasRefresh(data)) {
			queryTrailerList(1, "", "");
		}

	}

}
